package com.h3c.nem.controller;

import cn.hutool.json.JSONUtil;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.h3c.nem.annotation.CheckRole;
import com.h3c.nem.dto.TbOfferAttachmentDTO;
import com.h3c.nem.enums.RoleType;
import com.h3c.nem.service.ITbOfferAttachmentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * offer附件图片控制层
 *
 * @author chenyifan
 * @since 2022-06-02 17:14:07
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tbOfferAttachment")
public class TbOfferAttachmentController {
    /**
     * 服务对象
     */
    private final ITbOfferAttachmentService tbOfferAttachmentService;

    /**
     * 保存附件资料
     *
     * @param attachmentDTO
     * @return
     */
    @PostMapping("/saveAppendInfo")
    public Result<String> saveAppendInfo(@Valid @RequestBody TbOfferAttachmentDTO attachmentDTO) {
        try {
            return new Result(true, StatusCodeEnum.NORMAL, tbOfferAttachmentService.saveAppendInfo(attachmentDTO));
        } catch (Exception e) {
            log.error("保存附件资料失败:{}", JSONUtil.toJsonStr(e));
            return new Result(false, 400, e.getMessage());
        }
    }

    /**
     * 回显附件资料
     *
     * @return
     */
    @GetMapping("/showAppendInfo")
    public Result<String> showAppendInfo() {
        try {
            return new Result(true, StatusCodeEnum.NORMAL, tbOfferAttachmentService.showAppendInfo());
        } catch (Exception e) {
            return new Result(false, StatusCodeEnum.ADD_FAIl);
        }
    }

    /**
     * 更新未审核的头像附件
     *
     * @return
     */
    @GetMapping("/updateAvatarAttachmentInfo")
    public Result<String> updateAvatarAttachmentInfo(@RequestParam String avatarId,
                                                     @RequestParam String avatarName) {
        try {
            tbOfferAttachmentService.updateAvatarAttachmentInfo(avatarId, avatarName);
            return new Result(true, StatusCodeEnum.NORMAL);
        } catch (Exception e) {
            return new Result(false, 400, e.getMessage());
        }
    }


    /**
     * 更新人员工卡照片状态：通过、驳回
     *
     * @Author: h24002
     * @Date: 2022/6/24
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN,RoleType.USER_ROLE_CARD_DEPT})
    @ApiOperation(value = "更新人员工卡照片状态", notes = "更新人员工卡照片状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attachmentId", value = "附件ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phoneNo", value = "电话号码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isPass", value = "是否通过", required = true, dataType = "Boolean", paramType = "query"),
            @ApiImplicitParam(name = "rejectReason", value = "拒绝意见", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping("/updateWorkerCardImageStatus")
    public Result updateWorkerCardImageStatus(@RequestParam String attachmentId,
                                              @RequestParam String phoneNo,
                                              @RequestParam Boolean isPass,
                                              @RequestParam String rejectReason) {
        try {
            //先更新附件状态
            Boolean isOK = tbOfferAttachmentService.updateWorkerCardImageStatus(attachmentId, phoneNo, isPass, rejectReason);
            log.info("审核工卡照片(" + attachmentId + "," + phoneNo + "," + isPass + "," + rejectReason + ")结果：" + isOK + "");
            if (isOK) {
                return new Result(true, StatusCodeEnum.NORMAL, "更改审批状态成功");
            }
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), "更改审批状态失败");
        } catch (Exception ex) {
            log.info("审核工卡照片(" + attachmentId + "," + phoneNo + "," + isPass + "," + rejectReason + ")异常：", ex);
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }
}

